import asyncio

from parsel import Selector

"""
清理孤儿文档
"""
from re_common.baselibrary.utils.basemotor import BaseMotor

bs = BaseMotor()
bs.AsyncIOMotorClient(
    "mongodb://root:cqvipdatacenter@192.168.31.208:32118/admin",
    "admin")
# bs.get_col("jss_patent_page")
nextKey = {}


def hook(result):
    global nextKey
    print(result)
    if (result["ok"] != 1):
        print("Unable to complete at this time: failure or timeout.")
    nextKey = result["stoppedAtKey"]
    print(nextKey)


dicts = {"cleanupOrphaned": "testdb.jss_patent_page", "startingFromKey": nextKey}
print(dicts)
bs.run_common(hook, dicts)
asyncio.get_event_loop().run_until_complete(bs.run_common(hook, dicts))
while nextKey is not None and nextKey != {} and nextKey != '':
    dicts = {"cleanupOrphaned": "testdb.jss_patent_page", "startingFromKey": nextKey}
    print(dicts)
    bs.run_common(hook, dicts)
    asyncio.get_event_loop().run_until_complete(bs.run_common(hook, dicts))
